import Vue from 'vue'
import VueRouter from 'vue-router'
import Message from "element-ui"

Vue.use(VueRouter)

const routes = [{
  path: "/login",
  component: () => import("../pages/login"),
  meta:{
    title:"登录"
  }
}, {
  path: "/layout",
  component: () => import("../pages/layout"),
  children: [{
      path: "/home",
      component: () => import("../pages/home"),
      meta:{
        title:"首页"
      }
    }, {
      path: "/menu",
      component: () => import("../pages/menu"),
      meta:{
        title:"菜单管理"
      }
    },
    {
      path: "/role",
      component: () => import("../pages/role"),
      meta:{
        title:"角色管理"
      }
    },
    {
      path: "/admin",
      component: () => import("../pages/admin"),
      meta:{
        title:"管理员管理"
      }
    }, {
      path: "/cate",
      component: () => import("../pages/cate"),
      meta:{
        title:"商品分类"
      }
    },
    {
      path: "/specs",
      component: () => import("../pages/specs"),
      meta:{
        title:"商品规格"
      }
    }, {
      path: "/goods",
      component: () => import("../pages/goods"),
      meta:{
        title:"商品管理"
      }
    }, {
      path: "/member",
      component: () => import("../pages/member"),
      meta:{
        title:"会员管理"
      }
    }, {
      path: "/banner",
      component: () => import("../pages/banner"),
      meta:{
        title:"轮播图管理"
      }
    }, {
      path: "/seckill",
      component: () => import("../pages/seckill"),
      meta:{
        title:"秒杀活动"
      }
    }, {
      path: "",
      redirect: "/home"
    }
  ]
}, {
  path: "/404",
  component: () => import("../pages/pageNotFound"),
  meta:{
    title:"404"
  }
}, {
  path: "*",
  redirect: "/404"
}]

const router = new VueRouter({
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  if (to.path == "/login") {
    next()
    return
  }
  //判断是否登陆
  let info = JSON.parse(sessionStorage.getItem("admininfo") || "{}")
  if (!info.token) {
    next("/login")
  } else {
    info.menus_url.push("/home")
    if (info.menus_url.indexOf(to.path) == -1) {
      Message.error("您没有权限进行访问，请先去获取权限")
      console.log(info.menus_url.indexOf(item => item == to.path));
      return next("/home")
    }
    next()
  }
})

export default router